﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using DTC.Model;
namespace DTC
{
    public partial class FrmHistory : UIBase
    {
        public FrmHistory()
        {
            InitializeComponent();
        }
        
        private void buttonItem1_Click(object sender, EventArgs e)
        {

        }
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            QueryExcel qe = new QueryExcel() { actRefresh=UpdateXmlToListView};
            qe.Show();
        }
        private void UpdateXmlToListView()
        {
            listView1.Items.Clear();
            if (!File.Exists("FailRateStatic.xml"))
                return;
            LimitXml(20);
            XmlDocument doc = new XmlDocument(); 
            doc.Load("FailRateStatic.xml");
            XmlNode  root=doc.SelectSingleNode("Statistics");
            foreach (XmlNode node in root.ChildNodes)
            {
                string[] dtimes = node.Name.Split(new char[] { '-' });
                string BeginTime = ConverdtimeFormat(dtimes[0]);
                string EndTime = ConverdtimeFormat(dtimes[1]);
                ListViewItem lvi=new ListViewItem(BeginTime);
                lvi.SubItems.Add(EndTime);
                List<Statistics> ss = new List<Statistics>();
                foreach (XmlNode sub in node.ChildNodes)
                {
                    Statistics s = new Statistics();
                    s.Item = sub.Name;
                    foreach (XmlNode subsub in sub.ChildNodes)
                    {
                        if (subsub.Name == "PassQty")
                        {
                            s.PassQty = subsub.InnerText;
                        }                            
                        else if (subsub.Name == "FailQty")
                        {
                            s.FailQty = subsub.InnerText;
                        }
                        else if (subsub.Name == "TotalQty")
                        {
                            s.TotalQty = subsub.InnerText;
                        }
                        else if (subsub.Name == "FailRate")
                        {
                            s.FailRate = subsub.InnerText;
                        }
                    }
                    ss.Add(s);
                }
                lvi.Tag = ss;
                listView1.Items.Add(lvi);
            }
        }
        private void LimitXml(int count)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("FailRateStatic.xml");
            XmlNode root = doc.SelectSingleNode("Statistics");
            if(root.ChildNodes.Count>=count)
            {
                for(int i=0;i<root.ChildNodes.Count-count;i++)
                {
                    root.RemoveChild(root.ChildNodes[i]);
                }
                doc.Save("FailRateStatic.xml");
            }
        }
        private void FrmHistory_Load(object sender, EventArgs e)
        {
            UpdateXmlToListView();
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count>0)
            {
                ListViewItem lvi = listView1.SelectedItems[0];
                List<Statistics> ss = (List<Statistics>)lvi.Tag;          
                this.dgvStatistics.DataSource = new BindingList<Statistics>(ss); 
            }
            
        }
        private string ConverdtimeFormat(string dtime)
        {
            string year = dtime.Substring(0+1, 4);
            string month = dtime.Substring(4+1, 2);
            string day = dtime.Substring(6+1, 2);
            string hour = dtime.Substring(8+1, 2);
            string min = dtime.Substring(10+1, 2);
            string sec = dtime.Substring(12+1, 2);
            return string.Format("{0}-{1}-{2} {3}:{4}:{5}", year, month, day, hour, min, sec);
        }
    }
}
